//
//  NewfeatureController.swift
//  微博项目
//
//  Created by admin on 16/3/4.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewfeatureController: UICollectionViewController {
    // 重写构造方法
    override init(collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(collectionViewLayout: layout)
    }
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 便捷构造器
    convenience init()
    {
        // 设置布局
        let layout = UICollectionViewFlowLayout()
        // 设置cell的size
        layout.itemSize = UIScreen.mainScreen().bounds.size
        // 设置拖动方向
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        // 设置间距
        layout.minimumLineSpacing = 0
        // 初始化
        self.init(collectionViewLayout : layout)
        // 打开页面分页
        self.collectionView?.pagingEnabled = true
        // 不显示水平滚动条
        self.collectionView?.showsHorizontalScrollIndicator = false
        // 禁止弹簧属性
        self.collectionView?.bounces = false
    }
    var page:UIPageControl =
    {
        let temp = UIPageControl()
        let pageW:CGFloat = 100
        let pageH:CGFloat = 20
        temp.frame = CGRectMake((UIScreen.mainScreen().bounds.width - pageW) * 0.5, UIScreen.mainScreen().bounds.height * 0.9, pageW, pageH)
        temp.currentPageIndicatorTintColor = UIColor.orangeColor()
        temp.pageIndicatorTintColor = UIColor.darkGrayColor()
        return temp
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        // 初始化分也按钮
        self.page.numberOfPages = self.collectionView(self.collectionView!, numberOfItemsInSection: 0)
        self.view.addSubview(self.page)
        self.view.bringSubviewToFront(self.page)
        // 初始化最后一页的按钮
        self.shareButton.frame = CGRectMake((UIScreen.mainScreen().bounds.width - self.shareButton.bounds.size.width) * 0.5 + UIScreen.mainScreen().bounds.width * 3, UIScreen.mainScreen().bounds.height * 0.7 - self.shareButton.frame.size.height, self.shareButton.bounds.size.width, self.shareButton.frame.size.height)
        self.collectionView!.addSubview(self.shareButton)
        self.finishButton.frame = CGRectMake((UIScreen.mainScreen().bounds.width - self.finishButton.bounds.size.width) * 0.5 + UIScreen.mainScreen().bounds.width * 3, UIScreen.mainScreen().bounds.height * 0.7, self.finishButton.bounds.size.width, self.finishButton.frame.size.height)
        self.collectionView!.addSubview(self.finishButton)
        // 注册cell
        self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        // Do any additional setup after loading the view.
    }
    lazy var finishButton:UIButton =
    {
        let temp = UIButton(type: UIButtonType.Custom)
        temp.setBackgroundImage(UIImage(imageLiteral: "new_feature_finish_button"), forState: UIControlState.Normal)
        temp.setBackgroundImage(UIImage(imageLiteral: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        temp.sizeToFit()
        temp.setTitle("ok", forState: UIControlState.Normal)
        temp.frame = CGRectMake((UIScreen.mainScreen().bounds.width - temp.bounds.size.width) * 0.5, UIScreen.mainScreen().bounds.height * 0.7, temp.bounds.size.width, temp.frame.size.height)
        temp.addTarget(self, action: "finish", forControlEvents: UIControlEvents.TouchDown)
        return temp
    }()
    lazy var shareButton:UIButton =
    {
        let temp = UIButton(type: UIButtonType.Custom)
        temp.setBackgroundImage(UIImage(imageLiteral: "new_feature_share_false"), forState: UIControlState.Normal)
        temp.setBackgroundImage(UIImage(imageLiteral: "new_feature_share_true"), forState: UIControlState.Selected)
        temp.sizeToFit()
        temp.addTarget(self, action: "share", forControlEvents: UIControlEvents.TouchDown)
        return temp
    }()
    override func scrollViewDidScroll(scrollView: UIScrollView) {
        self.page.currentPage = Int((scrollView.contentOffset.x + self.collectionView!.bounds.width * 0.5) / self.collectionView!.bounds.width)
    }
    func share()
    {
        self.shareButton.selected = !self.shareButton.selected
    }
    func finish()
    {
        let user = NSUserDefaults.standardUserDefaults()
        user.setObject(NSBundle.mainBundle().infoDictionary!["CFBundleVersion"]!, forKey: versionKey)
        user.synchronize()
        ChoiceController.chooseController()
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    deinit{
        print("销毁啦！！！")
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    // MARK: UICollectionViewDataSource

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 4
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
        let image = UIImageView(frame: cell.bounds)
        image.image = UIImage(imageLiteral: "new_feature_\(indexPath.row + 1)")
        cell.backgroundView = image
        // Configure the cell
        return cell
    }

    // MARK: UICollectionViewDelegate

    /*
    // Uncomment this method to specify if the specified item should be highlighted during tracking
    override func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    */

    /*
    // Uncomment this method to specify if the specified item should be selected
    override func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    */

    /*
    // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
    override func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
        return false
    }

    override func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
        return false
    }

    override func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
    
    }
    */

}
